package com.ruoyi.team.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.team.domain.GoodsStore;
import com.ruoyi.team.service.IGoodsStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.team.mapper.GoodsMapper;
import com.ruoyi.team.domain.Goods;
import com.ruoyi.team.service.IGoodsService;

/**
 * 物品信息Service业务层处理
 *
 * @author 东
 * @date 2022-11-28
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Autowired
    private IGoodsStoreService goodsStoreService;
    /**
     * 查询物品信息列表
     *
     * @param goods 物品信息
     * @return 物品信息
     */
    @Override
    public List<Goods> selectGoodsList(Goods goods) {
        List<Goods> goodsList = getBaseMapper().selectGoodsList(goods);

        for (Goods currentGoods : goodsList) {
            // 基于 物品ID 得到 goodsStore 对象
            List<GoodsStore> goodsStoreList = goodsStoreService.getBaseMapper().selectList(Wrappers.lambdaQuery(GoodsStore.class).eq(GoodsStore::getGoodsId, currentGoods.getId()));
            // 基于 goodsStore对象 得到 该物品的数量
            List<Long> goodsStoreAmountsList = goodsStoreList.stream().map(GoodsStore::getAmounts).collect(Collectors.toList());
            // 把所有物品数量相加得到总数量
            Long goodsStoreCountAmounts = goodsStoreAmountsList.stream().reduce(Long::sum).orElse(0L);
            // 再把总数量设置进去
            currentGoods.setAmounts(goodsStoreCountAmounts);
        }
        return goodsList;
    }

    @Override
    public List<Goods> selectGoodsListByCategoryId(Long categoryId) {
        return getBaseMapper().selectGoodsListByCategoryId(categoryId);
    }
}
